﻿/* DotNetHoldem 
 * .Net Solution that allows to use OpenHoldem with .Net logic
 * Author: Óscar Andreu
 * oesido at gmail dot com
 * Licensed under GPLv3 - 2012
 */

using System;
using System.Collections.Generic;
using DotNetBotLogic.Classes;
using DotNetBotLogic.Controllers;
using DotNetBotLogic.Enums;
using DotNetBotLogic.Classes.Log;

namespace DotNetBotLogic
{
    public class MainController
    {
        #region static

        private static int handsPlayed;
        internal int HandsPlayed
        {
            get { return handsPlayed; }
            set
            {
                if (handsPlayed != value)
                {
                    handsPlayed++;
                    betRound = 0;
                    playTrackerController.PrepareNewHand();

                    Logger.Log(string.Format("\n ------------- NEW HAND ({0})-----------\n", handsPlayed));
                }

                handsPlayed = value;
            }
        }

        private static BetRounds betRound;
        internal static BetRounds BetRound
        {
            get { return betRound; }
        }

        /// <summary>
        /// Betting round inside a BetRound
        /// </summary>
        internal static int BetTurn = 1;

        #endregion

        #region Variables

        private SwagController swagController = null;
        internal SwagController SwagController
        {
            get { return swagController ; }
        }

        private CallController callController = null;
        internal CallController CallController
        {
            get { return callController == null ? new CallController(this) : callController; }
        }

        private PlayTrackerController playTrackerController = null;
        internal PlayTrackerController PlayTrackerController
        {
            get { return playTrackerController; }
        }

        #endregion

        #region Constructor

        public MainController(IntPtr getSymbolFromDll, IntPtr getPlayerName)
        {
            OhHelper.Oh = new OH_Wrapper(getSymbolFromDll, getPlayerName);

            swagController = new SwagController(this);
            callController = new CallController(this);
            playTrackerController = new PlayTrackerController(this);
        }

        #endregion

        /// <summary>
        /// Process de query message
        /// </summary>
        /// <param name="pquery"></param>
        /// <returns></returns>S
        public double ProcessQuery(string pquery)
        {
            double lResult = 0;
            // look at google for c++ struct to c#
            //double pot = oh.getsym("potcommon");
            //holdem_state state = OhHelper.GetHoldemState();
            //Console.WriteLine(state.m_title);

            if (pquery == "dll$swag")
            {
                lResult = SwagController.GetActionResult();
            }
            else if (pquery == "dll$call")
            {
                lResult = CallController.GetActionResult();
            }
            else if (pquery == "dll$alli")
            {
                HandsPlayed = (int) OhHelper.GetSymbol(Symbols.handsplayed);
                SetBetRound(OhHelper.GetBetRound());
                //playTrackerController.GetActionResult(true);

                lResult = 0;
            }
            else if (pquery == "dll$srai")
            {
                lResult = 0;
            }
            else if (pquery == "ddll$rais")
            {
                lResult = 0;
            }
            else if (pquery == "dll$prefold")
            {
                lResult = 0;
            }
            else if (pquery == "dll$rebuy")
            {
                lResult = 0;
            }
            else if (pquery == "dll$delay")
            {
                lResult = 0;
            }
            else if (pquery == "dll$chat")
            {
                lResult = 0;
            }
            else if (pquery == "dll$play")
            {
                //playTrackerController.GetActionResult(false);

                lResult = 1;
            }

            return lResult;
        }

        private void SetBetRound(BetRounds br)
        {
            if (betRound != br)
            {
                BetTurn = 1;
                BaseController.BBlind = OhHelper.GetSymbol(Symbols.bblind);
                playTrackerController.PrepareNewBetRound();
            }
            else
                BetTurn++;

            betRound = br;
        }
    }
}
